package com.backstage.webSocket;

import org.apache.shiro.SecurityUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;


public class ChatHandshakeInterceptor extends HttpSessionHandshakeInterceptor {
    @Override
    public boolean beforeHandshake(
        ServerHttpRequest request,
        ServerHttpResponse response,
        WebSocketHandler wsHandler,
        Map<String, Object> attributes
    ) throws Exception {
        System.out.println("Before Handshake / 握手前");

        //解决The extension [x-webkit-deflate-frame] is not supported问题
        //其中标红色的代码是为了解决下面的错误，可能是ios手机safari浏览器版本问题引起的。
        //org.springframework.web.socket.server.HandshakeFailureException: Uncaught failure for request http://localhost:8080/spring4/myHandler; nested exception is java.lang.IllegalArgumentException: The extension [x-webkit-deflate-frame] is not supported
        if(request.getHeaders().containsKey("Sec-WebSocket-Extensions")) {
            request.getHeaders().set("Sec-WebSocket-Extensions", "permessage-deflate");
        }

        if (request instanceof ServletServerHttpRequest) {
            ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;
            HttpSession session = servletRequest.getServletRequest().getSession(false);
            if (session != null) {
                //使用userName区分WebSocketHandler，以便定向发送消息
                //String userID = session.getAttribute("SESSION_USERID").toString();  //一般直接保存user实体
                String chatID = session.getAttribute("CHAT_ID").toString();  //存储当前用户进入的 聊天室 编号 RANGE_ID
                if (chatID!=null) {
                    attributes.put("WEBSOCKET_CHATID",chatID);
                }
            }
        }
        return super.beforeHandshake(request, response, wsHandler, attributes);

        //使用userName区分WebSocketHandler，以便定向发送消息(使用shiro获取session,或是使用上面的方式)
        //String userName = (String) SecurityUtils.getSubject().getSession().getAttribute("SESSION_USERNAME");
        //if (userName == null) {
        //    userName = "default-system";
        //}
        //attributes.put("WEBSOCKET_USERNAME", userName);
        //return super.beforeHandshake(request, response, wsHandler, attributes);
    }

    @Override
    public void afterHandshake(
        ServerHttpRequest request,
        ServerHttpResponse response,
        WebSocketHandler wsHandler,
        Exception ex
    ){
        System.out.println("After Handshake / 握手后");
        super.afterHandshake(request, response, wsHandler, ex);
    }

}
